package com.gwz.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gwz.sys.constast.SYSConstast;
import com.gwz.sys.domain.Dept;
import com.gwz.sys.domain.Permission;
import com.gwz.sys.domain.User;
import com.gwz.sys.service.PermissionService;
import com.gwz.sys.utils.DataGridView;
import com.gwz.sys.utils.ResultObj;
import com.gwz.sys.utils.TreeNode;
import com.gwz.sys.utils.TreeNodeBuilder;
import com.gwz.sys.utils.WebUtils;
import com.gwz.sys.vo.DeptVo;
import com.gwz.sys.vo.PermissionVo;

@RestController
@RequestMapping("menu")
public class MenuController {

	@Autowired
	private PermissionService permissionService;
	
	
	@RequestMapping("loadIndexLeftMenu")
	public List<TreeNode> loadIndexLeftMenu(){
		User user = WebUtils.getCurrentUser();
		List<Permission> menus = null;
		PermissionVo permissionVo = new PermissionVo();
		permissionVo.setAvailable(SYSConstast.SYS_AVAILABLE_TRUE);
		permissionVo.setType(SYSConstast.PERMISSION_TYPE_MEUN);
		if (user.getType()==SYSConstast.USER_TYPE_SUPER) {
			menus = permissionService.queryAllPermissionForList(permissionVo);
		}else {
			menus = permissionService.queryPermissionByUserIdForList(permissionVo, user.getId());
		}
		List<TreeNode> treeNodes = new ArrayList<>();
		for (Permission p : menus) {
			Integer id = p.getId();
			Integer pid = p.getPid();
			String name = p.getName();
			String icon = p.getIcon();
			String href = p.getHref();
			Boolean spread = p.getOpen() == SYSConstast.SYS_OPEN_TRUE?true:false;
			treeNodes.add(new TreeNode(id, pid, name, icon, href, spread));
		}
		return TreeNodeBuilder.builder(treeNodes, 1);
	}
	
	@RequestMapping("loadMenuLeftTreeJson")
	public List<TreeNode> loadMenuLeftTreeJson(PermissionVo permissionVo){
		permissionVo.setAvailable(SYSConstast.SYS_AVAILABLE_TRUE);
		List<Permission> list = permissionService.queryAllPermissionForList(permissionVo);
		List<TreeNode> treeNodes = new ArrayList<>();
		for (Permission p : list) {
			Boolean open = p.getOpen() == SYSConstast.SYS_OPEN_TRUE?true:false;
			Boolean isParent = p.getParent() == SYSConstast.SYS_PARENT_TRUE?true:false;
			treeNodes.add(new TreeNode(p.getId(), p.getPid(), p.getName(), open, isParent));
		}
		return treeNodes;
	}
	
	
	@RequestMapping("loadAllMenu")
	public DataGridView loadAllMenu(PermissionVo permissionVo) {
		permissionVo.setAvailable(SYSConstast.SYS_AVAILABLE_TRUE);
		permissionVo.setType(SYSConstast.PERMISSION_TYPE_MEUN);
		return permissionService.queryAllPermission(permissionVo);
	}
	

	@RequestMapping("loadMaxOrderNumber")
	public Integer loadMaxOrderNumber() {
		Integer num=this.permissionService.queryMaxOrderNumber();
		return num+1;
	}
	
	
	@RequestMapping("addMenu")
	public ResultObj addMenu(PermissionVo permissionVo) {
		ResultObj resultObj = new ResultObj(200, "添加成功");
		try {
			permissionVo.setType(SYSConstast.PERMISSION_TYPE_MEUN);
			permissionService.addPermission(permissionVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("添加失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	
	@RequestMapping("updateMenu")
	public ResultObj updateMenu(PermissionVo permissionVo) {
		ResultObj resultObj = new ResultObj(200, "修改成功");
		try {
			permissionService.updatePermission(permissionVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("修改失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	
	@RequestMapping("isCurrentMenuHasChildrenNode")
	public ResultObj isCurrentMenuHasChildrenNode(Integer id) {
		Integer childrenCount=this.permissionService.isCurrentPermissionHasChildrenNode(id);
		if(childrenCount>0) {
			return new ResultObj(-1, "当前菜单有子菜单，请先删除子菜单");
		}else {
			return new ResultObj(200, "");
		}
	}
	
	
	@RequestMapping("deleteMenu")
	public ResultObj deleteMenu(PermissionVo permissionVo) {
		ResultObj resultObj = new ResultObj(200, "删除成功");
		try {
			permissionService.deletePermission(permissionVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("删除失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	

}
